package test2;

public class Test5 {
    public static void main(String[] args) {
        int[] nums = new int[]{2, 5, 4, 6, 1, 7};
        int[] arr = quick_sort(nums, 0, nums.length - 1);
        for (int i : arr) {
            System.out.print(i + " ");
        }
    }

    private static int[] quick_sort(int[] nums, int begin, int end) {
        if (begin > end) {
            return nums;
        }
        int left = begin;
        int right = end;
        int key = left;
        while (left < right) {
            while (left < right && nums[right] >= nums[key]) {
                right--;
            }
            while (left < right && nums[left] <= nums[key]) {
                left++;
            }
            if (left < right) {
                swap(nums, left, right);
            }
        }
        int meet = left;
        swap(nums, meet, key);
        quick_sort(nums, left, meet - 1);
        quick_sort(nums, meet + 1, right);
        return nums;
    }

    private static void swap(int[] nums, int left, int right) {
        int tmp = nums[left];
        nums[left] = nums[right];
        nums[right] = tmp;
    }
}
